package com.lzq.course.system.controller.admin;

import com.lzq.course.server.domain.Resource;
import com.lzq.course.server.dto.ResponseDto;
import com.lzq.course.server.service.ResourceService;
import com.lzq.course.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController {

    public static final String BUSINESS_NAME = "资源";

    @javax.annotation.Resource
    private ResourceService resourceService;

    /**
     * 查询列表
     * @return
     */
    @PostMapping("/list")
    public ResponseDto list() {
        ResponseDto responseDto = new ResponseDto();
        List<Resource> list = resourceService.list();

        responseDto.setContent(list);
        return responseDto;
    }

    /**
     * 保存
     * @param jsonStr 资源配置字符串
     * @return
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody String jsonStr) {
        //保存校验
        ValidatorUtil.require(jsonStr, "资源");

        ResponseDto responseDto = new ResponseDto();
        resourceService.saveJson(jsonStr);

        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        resourceService.delete(id);

        return responseDto;
    }
}
